<?php

$DECKROOT = getcwd();

require $DECKROOT . '/config/config.php';
include $DECKROOT . '/classes/database/'.$dbType.".php";

$conn = new mysqli($dbHost,$dbUser,$dbPass,$dbName);

$createTableQuery = array();


$createTableQuery = array(
		array("table" => "pmtdeck", "query" => "CREATE TABLE pmtdeck (id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), description VARCHAR(2500), shortDescription CHAR(144), status TINYINT UNSIGNED, themeID MEDIUMINT UNSIGNED);"),
		array("table" => "pmtcard", "query" => "CREATE TABLE pmtcard (id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), description VARCHAR(2500), shortDescription CHAR(144), status TINYINT UNSIGNED, stackID MEDIUMINT UNSIGNED,parentID MEDIUMINT UNSIGNED,value MEDIUMINT UNSIGNED,assignedTo MEDIUMINT UNSIGNED,effort MEDIUMINT UNSIGNED);"),
		array("table" => "pmtstack", "query" => "CREATE TABLE pmtstack (id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), description VARCHAR(2500), shortDescription CHAR(144), status TINYINT UNSIGNED, deckID MEDIUMINT UNSIGNED);"),
);

echo  nl2br(count($createTableQuery)." tables must be created"."\r\n\r\n");

foreach ($createTableQuery as $query) {
	$tableName = $query [ 'table' ];
	
	echo nl2br("testing table '".$tableName."' if it already exists........\r\n");
	
	
	$Result=$conn->query("SHOW TABLES LIKE '".$tableName."';");
	if(mysqli_num_rows($Result)==1) {
		echo  nl2br("the table exists. Do nothing.\r\n\r\n");
	}
	else {
		echo nl2br("the table does not exist. Create it.\r\n");
		$Createquery = $query [ 'query' ];
		
		if($conn->query($Createquery)){
			echo nl2br("Succesfully created table \r\n\r\n") ;
		} else {
			echo nl2br("Unsuccesfully not created table \r\n\r\n");
		}
	}			
}

/* commit transaction */
if (!$conn->commit()) {
	echo ("Transaction commit failed\n");
	exit();
}
$conn->close();

?>